// ==================================================================================================
// Copyright(C) 2018 FI Ltd. All rights reserved.
//
// SYSTEM NAME	   : FI.Net Application Framework 
// COMPONENT ID	   : FI.Helpers.Data.DataAccessConfigurationView
// COMPONENT DESC  : Represents a view to navigate the configuration data.	
//
// CREATED DATE/BY : 18 Aug 2018 / Tian Mi
//
// REVISION HISTORY:	DATE/BY			SR#/CS/PM#/OTHERS		DESCRIPTION OF CHANGE
// ==================================================================================================

using System.Configuration;

using FI.Helpers.Data.Configuration;
using FI.Core;
using System;
using FI.Helpers.Data.Properties;

namespace FI.Helpers.Data
{
    /// <summary>   
    /// Represents a view to navigate the <see cref="DataAccessSettings"/> configuration data.
    /// </summary>   
    public static class DataAccessConfigurationView
    {
        private static readonly DataAccessSettings settings = ConfigManager.GetConfigurationSection<DataAccessSettings>();

        /// <summary>
        /// Gets the field name of version no in database.
        /// </summary>
        /// <value>The version no field.</value>
        public static string VersionNoField
        {
            get { return GetVersionNoField(); }
        }

        /// <summary>
        /// Gets the field name of transaction id in database.
        /// </summary>
        /// <value>The transaction id field.</value>
        public static string TransactionIdField
        {
            get { return GetTransactionIdField(); }
        }

        /// <summary>
        /// Gets the field name of created by in database.
        /// </summary>
        /// <value>The created by field.</value>
        public static string CreatedByField
        {
            get { return GetCreatedByField(); }
        }

        /// <summary>
        /// Gets the field name of created date in database.
        /// </summary>
        /// <value>The created time field.</value>
        public static string CreatedTimeField
        {
            get { return GetCreatedTimeField(); }
        }

        /// <summary>
        /// Gets the field name of last updated by in database.
        /// </summary>
        /// <value>The last updated by field.</value>
        public static string LastUpdatedByField
        {
            get { return GetLastUpdatedByField(); }
        }

        /// <summary>
        /// Gets the field name of last updated date in database.
        /// </summary>
        /// <value>The last updated time field.</value>
        public static string LastUpdatedTimeField
        {
            get { return GetLastUpdatedTimeField(); }
        }

        /// <summary>
        /// Gets the format of parameter which mapping to the current version of datacolumn.
        /// </summary>
        /// <value>The current parameter format.</value>
        public static string CurrentParameterFormat
        {
            get { return GetCurrentParameterFormat(); }
        }

        /// <summary>
        /// Gets the format of parameter which mapping to the original version of datacolumn.
        /// </summary>
        /// <value>The original parameter format.</value>
        public static string OriginalParameterFormat
        {
            get { return GetOriginalParameterFormat(); }
        }



        /// <summary>
        /// Gets the field name of version no in database.
        /// </summary>
        /// <value>The version no field.</value>
        public static string GetVersionNoField(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            SystemFieldData data = dataAccessSettings.SystemFields.Get(SystemFieldData.VersionNo);
            if (data == null)
            {
                return string.Empty;
            }
            return data.FieldName;
        }

        /// <summary>
        /// Gets the field name of transaction id in database.
        /// </summary>
        /// <value>The transaction id field.</value>
        public static string GetTransactionIdField(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            SystemFieldData data = dataAccessSettings.SystemFields.Get(SystemFieldData.TransactionId);
            if (data == null)
            {
                return string.Empty;
            }
            return data.FieldName;
        }

        /// <summary>
        /// Gets the field name of created by in database.
        /// </summary>
        /// <value>The created by field.</value>
        public static string GetCreatedByField(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            SystemFieldData data = dataAccessSettings.SystemFields.Get(SystemFieldData.CreatedBy);
            if (data == null)
            {
                return string.Empty;
            }
            return data.FieldName;
        }

        /// <summary>
        /// Gets the field name of created date in database.
        /// </summary>
        /// <value>The created time field.</value>
        public static string GetCreatedTimeField(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            SystemFieldData data = dataAccessSettings.SystemFields.Get(SystemFieldData.CreatedTime);
            if (data == null)
            {
                return string.Empty;
            }
            return data.FieldName;
        }

        /// <summary>
        /// Gets the field name of last updated by in database.
        /// </summary>
        /// <value>The last updated by field.</value>
        public static string GetLastUpdatedByField(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            SystemFieldData data = dataAccessSettings.SystemFields.Get(SystemFieldData.LastUpdatedBy);
            if (data == null)
            {
                return string.Empty;
            }
            return data.FieldName;
        }

        /// <summary>
        /// Gets the field name of last updated date in database.
        /// </summary>
        /// <value>The last updated time field.</value>
        public static string GetLastUpdatedTimeField(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            SystemFieldData data = dataAccessSettings.SystemFields.Get(SystemFieldData.LastUpdatedTime);
            if (data == null)
            {
                return string.Empty;
            }
            return data.FieldName;
        }

        /// <summary>
        /// Gets the default format of select stored procedure.
        /// </summary>
        /// <value>The select procedure format.</value>
        public static string GetSelectProcedureFormat(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            CommandData data = dataAccessSettings.Commands.Get(CommandData.Select);
            
            return data.Format;
        }

        /// <summary>
        /// Gets the default format of update stored procedure.
        /// </summary>
        /// <value>The update procedure format.</value>
        public static string GetUpdateProcedureFormat(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            CommandData data = dataAccessSettings.Commands.Get(CommandData.Update);
            
            return data.Format;
        }

        /// <summary>
        /// Gets the default format of insert stored procedure.
        /// </summary>
        /// <value>The insert procedure format.</value>
        public static string GetInsertProcedureFormat(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            CommandData data = dataAccessSettings.Commands.Get(CommandData.Insert);
            
            return data.Format;
        }

        /// <summary>
        /// Gets the default format of delete stored procedure.
        /// </summary>
        /// <value>The delete procedure format.</value>
        public static string GetDeleteProcedureFormat(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            CommandData data = dataAccessSettings.Commands.Get(CommandData.Delete);
            
            return data.Format;
        }

        /// <summary>
        /// Gets the format of parameter which mapping to the current version of datacolumn.
        /// </summary>
        /// <value>The current parameter format.</value>
        public static string GetCurrentParameterFormat(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            ParameterData data = dataAccessSettings.Parameters.Get(ParameterData.Current);
            
            return data.Format;
        }

        /// <summary>
        /// Gets the format of parameter which mapping to the original version of datacolumn.
        /// </summary>
        /// <value>The original parameter format.</value>
        public static string GetOriginalParameterFormat(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            ParameterData data = dataAccessSettings.Parameters.Get(ParameterData.Original);
            
            return data.Format;
        }

        /// <summary>
        /// Gets the mapping format of datatable.
        /// </summary>
        /// <value>The data table mapping.</value>
        public static string GetDataTableMapping(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            SchemaMappingData data = dataAccessSettings.SchemaMappings.Get(SchemaMappingData.DataTableName);
            
            return data.Format;
        }

        /// <summary>
        /// Gets the mapping format of datacolumn.
        /// </summary>
        /// <value>The data column mapping.</value>
        public static string GetDataColumnMapping(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            SchemaMappingData data = dataAccessSettings.SchemaMappings.Get(SchemaMappingData.DataColumnName);
            
            return data.Format;
        }

        /// <summary>
        /// Gets the command time out value.
        /// </summary>
        /// <value>The command time out.</value>
        public static int GetCommondTimeout(DataAccessSettings dataAccessSettings = null)
        {
            dataAccessSettings = dataAccessSettings ?? settings;
            TimeSpan span;
            if (!TimeSpan.TryParse(dataAccessSettings.CommandTimeout, out span))
            {
                ExceptionHelper.ThrowConfigurationErrorsException(string.Format(Resources.Culture, Resources.TimeSpanFormateError));
            }
            return (int)span.TotalSeconds;
        }
    }
}